package cn.iocoder.yudao.module.aiposter.service.material;

import cn.iocoder.yudao.module.aiposter.dal.mysql.material.AppMaterialMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.aiposter.controller.app.material.vo.*;
import cn.iocoder.yudao.module.aiposter.dal.dataobject.material.MaterialDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.aiposter.enums.ErrorCodeConstants.*;

/**
 * 素材资源 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AppMaterialServiceImpl implements AppMaterialService {

    @Resource
    private AppMaterialMapper appMaterialMapper;

    @Override
    public Long createMaterial(AppMaterialSaveReqVO createReqVO) {
        // 插入
        MaterialDO material = BeanUtils.toBean(createReqVO, MaterialDO.class);
        appMaterialMapper.insert(material);
        // 返回
        return material.getId();
    }

    @Override
    public void updateMaterial(AppMaterialSaveReqVO updateReqVO) {
        // 校验存在
        validateMaterialExists(updateReqVO.getId());
        // 更新
        MaterialDO updateObj = BeanUtils.toBean(updateReqVO, MaterialDO.class);
        appMaterialMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterial(Long id) {
        // 校验存在
        validateMaterialExists(id);
        // 删除
        appMaterialMapper.deleteById(id);
    }

    private void validateMaterialExists(Long id) {
        if (appMaterialMapper.selectById(id) == null) {
            throw exception(MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public MaterialDO getMaterial(Long id) {
        return appMaterialMapper.selectById(id);
    }

    @Override
    public PageResult<MaterialDO> getMaterialPage(AppMaterialPageReqVO pageReqVO) {
        return appMaterialMapper.selectPage(pageReqVO);
    }

}